<!DOCTYPE html>
<%@page import="com.surelution.projects.crm.User"%>
<%@page import="com.surelution.projects.crm.UserPageEntry"%>
<%@page import="com.surelution.projects.crm.PageEntry"%>
<html>
    <head>
        <title><g:layoutTitle default="Grails" /></title>
		<resource:accordion skin="default" />
		<resource:treeView />
        <link rel="stylesheet" href="${resource(dir:'css',file:'main.css')}" />
        <link rel="shortcut icon" href="${resource(dir:'images',file:'favicon.ico')}" type="image/x-icon" />
        <g:layoutHead />
        <g:javascript library="application" />
        <style type='text/css' media='screen'>
			dl.accordion-menu dd.a-m-d .bd {
				padding: 0px;
			}
			* html .clearfix { height: 1%; }
			 .ui-accordion-content { 
			 	zoom: 1;
			 }
			 $("#accordion").accordion({ header: "h3", alwaysOpen: false, active: false, autoHeight: false});
		</style>
		<script type="text/javascript">
			function pageEntry(id){
				var thisURL = document.URL; 
				var pageSize = thisURL.split( "/" );
				var thisHPage = pageSize[ pageSize.length-2]; 
				alert(thisHPage);
				window.location.href='${createLinkTo(dir: '/')}'+id ;
			}
		</script>
    </head>
    <g:set var="loggedUserId"><sec:loggedInUserInfo field="id"/></g:set>
    <%
		def cats
		def authPages
		def data1
		if(loggedUserId) {
			def user = User.get(loggedUserId.toString())
			
			def userPageEntries = UserPageEntry.findAllByUser(user)
			def menus = new HashSet()
			userPageEntries.each {
				menus.add(it.entry.parent)
			}
			
			authPages = userPageEntries.collect {
				it.entry
			}
			
			menus.each {entry->
				entry.children.each {child->
					def show = authPages.contains(entry)
					entry.metaClass.getShow {show}
				}
			}
			cats = menus.sort {
				it.positionSeq
			}

			def writer = new StringWriter()
			def xml = new groovy.xml.MarkupBuilder(writer)
			def list = location?location:PageEntry.get(1)
			xml.bookings(name: "${list.label}"){
				userPageEntries.each {page->
					if (page.entry.parent == list){
						book(name:"${page.entry.label}",id:"${page.entry.controllerName}/${page.entry.actionName}"){}
					}
				}
			}
			data1 = writer.toString()
		}
	 %>
    <body>
	    <g:set var="currPage" value="${PageEntry.findByControllerNameAndActionName(controllerName, actionName) }"/>
	    <div id="container">
	        <div id="header">
	        	<a href="${createLink(uri: '/overview/index')}"><img src="${resource(dir:'images',file:'pc.png')}" border="0" /></a>
	        	<div style='float: right;font-size:12px;color:black;'>
	        	<sec:ifLoggedIn>当前用户：<sec:username />&nbsp;&nbsp;<g:link controller="user" action="showChangePw" style="color:black;font-weight:normal;">更改密码</g:link>&nbsp;&nbsp;<g:link controller="logout" style="color:black;font-weight:normal;">退出</g:link></sec:ifLoggedIn>
				<sec:ifNotLoggedIn><g:link controller="login" action="auth" style="color:black;font-weight:normal;">登录</g:link> </sec:ifNotLoggedIn>
				</div>
	        </div><%--
	        <div id="titleCol" style="width:100%;border:2px solid black;height:30px;background:#6495ED;">
		        <g:each in="${cats }" var="cat">
		        	<div id="titlePart" style="width:150px;height:30px;display:inline;font-size:20px;color:white;padding:0 5px;">
		        		<g:link controller="location" action="index" style="color:white;font-weight:normal;" id="${cat.id}">${cat.label }</g:link>
		        	</div>
		        	<div style="display:inline;font-size:20px;">|</div>
	        	</g:each>
	        </div>
	        --%>
	        <sec:ifNotLoggedIn>
		        <div id="leftcol" >
					<form action='/petro-crm/j_spring_security_check' method='POST' id='loginForm' class='cssform' autocomplete='off'>
						<p >
							<label for='username' style="color:white;"><g:message code="springSecurity.login.username.label"/>:</label>
							<input type='text' class='text_' style="width:80px;" name='j_username' id='username'/>
						</p>
			
						<p>
							<label for='password'  style="color:white;"><g:message code="springSecurity.login.password.label"/>:</label>
							<input type='password' class='text_' style="width:80px;" name='j_password' id='password'/>
						</p>
			
						<p id="remember_me_holder">
							<input type='checkbox' class='chk' name='${rememberMeParameter}' id='remember_me' <g:if test='${hasCookie}'>checked='checked'</g:if>/>
							<label for='remember_me' style="color:white;"><g:message code="springSecurity.login.remember.me.label"/></label>
							<input type='submit' id="submit" value='${message(code: "springSecurity.login.button")}' class='Isubmit'/>
						</p>
					</form>
				</div>
			</sec:ifNotLoggedIn>
	        
	        <sec:ifLoggedIn><div id="leftcol">
				  <richui:accordion style="width:100%;" class="clearfix">
					<g:each in="${cats }" var="cat">
						<richui:accordionItem id="menu_cat_${cat.id }" caption="${cat.label }" expanded="${currPage?.parent?.id==cat.id }">
							<g:each in="${cat.children.findAll({it.showInMenu}).sort {it.positionSeq}}" var="item">
								<g:if test="${authPages.collect{it.id}.contains(item.id) }">
									<p style="width:100%;background:#1b88f8;border:1px solid black;">
										<g:link controller="${item.controllerName }" action="${item.actionName }">${item.label }</g:link>
									</p>
									</g:if>
							</g:each>
						</richui:accordionItem>
					</g:each>
					</richui:accordion>		
					
				<%--<richui:treeView xml="${data1}" onLabelClick="pageEntry(id)"/>

				<richui:treeView id="tree" xml="${data1}" onLabelClick="if(id) window.location.href='${createLinkTo(dir: '/')}'+id ;" showRoot=false; />--%>
	        </div></sec:ifLoggedIn>
			<div id="main_content"><g:layoutBody /></div>
		</div>
    </body>
</html>